package top.infopub.security.core;


import java.util.Collection;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.infopub.cache.RedisCached;


/**
 * 实现shiro的缓存处理 put get方法等
 * @author Awoke
 * @version 2018年9月17日
 * @see ShiroRedisCache
 * @since
 */
public class ShiroRedisCache<K, V> implements Cache<K, V> {

    private static Logger logger = LoggerFactory.getLogger(ShiroRedisCache.class);

    private String name;

    private RedisCached cached;

    /**
     * 获得byte[]型的key
     * 
     * @param key
     * @return
     */
    private String getKey(K key) {
        if (key instanceof String) {
            return key.toString();
        }
        else {
            return key.toString();
        }
    }

    private String getByteName() {
        return name;

    }

    @SuppressWarnings("unchecked")
    @Override
    public V get(K key)
        throws CacheException {
        logger.debug("Hash根据key从Redis中获取对象 key [" + key + "]");
        try {
            if (key == null) {
                return null;
            }
            else {
                V value = (V)cached.getHashCached(getByteName(), getKey(key));
                return value;
            }
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }

    }

    @Override
    public V put(K key, V value)
        throws CacheException {
        logger.debug("Hash根据key从存储 key [" + key + "]");
        try {
            cached.updateHashCached(getByteName(), getKey(key), value);
            return value;
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public V remove(K key)
        throws CacheException {
        logger.debug("Hash从redis中删除[" + getByteName() + "] key [" + key + "]");
        try {
            V previous = get(key);
            cached.deleteHashCached(getByteName(), getKey(key));
            return previous;
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public void clear()
        throws CacheException {
        logger.debug("从redis中删除所有元素");
        try {
            cached.deleteCached(getByteName());
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public int size() {
        try {
            Long longSize = new Long(cached.getHashSize(getByteName()));
            return longSize.intValue();
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Set<K> keys() {
        try {
            Set<K> keys = cached.getHashKeys(getByteName());
            return keys;
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Collection<V> values() {
        try {
            Collection<V> values = cached.getHashValues(getByteName());
            return values;
        }
        catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RedisCached getCached() {
        return cached;
    }

    public void setCached(RedisCached cached) {
        this.cached = cached;
    }

}